package com.haojiu.pojo;


import com.haojiu.util.HttpUtils;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:我叫浩九
 * @date：2021/8/31 16:08
 * create in RuGao JiangSu
 */
@Component
@Data
public class SmsComponent {
    private String host="https://gyytz.market.alicloudapi.com";
    private String path="/sms/smsSend";
    private String method="POST";
    private String appcode="20bb84e1305c414ca4d42e9a5dcb2cba";
    private String smsSignId="2e65b1bb3d054466b82f0c9d125465e2";
    private String templateId="908e94ccf08b4476ba6c876d13f084ad";
    public void sendSmsCode(String phone, String code){
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("param", "**code**:"+code+",**minute**:5");
        querys.put("smsSignId", smsSignId);
        querys.put("templateId", templateId);
        Map<String, String> bodys = new HashMap<String, String>();

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}